<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1000px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
      <el-row>
        <!-- <el-col :span="12">
          <el-form-item label="项目" prop="projectid" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit"></el-button>
            </el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="合同" prop="contractid">
            <el-input v-model="model.dispcontractid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmContractRefDialog" v-if="!disableEditSubmit"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="变更原因" prop="changereason" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.changereason" type="textarea" :rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同所属组织" prop="contractorgname">
            <el-input v-model="model.contractorgname" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同所属部门" prop="contractdeptname">
            <el-input v-model="model.contractdeptname" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同日期" prop="contractdate">
            <el-date-picker v-model="model.contractdate" type="date" placeholder="合同日期" value-format="yyyy-MM-dd" :disabled="true"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="customername">
            <el-input v-model="model.customername" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同金额(原)" prop="contractmoney">
            <el-input-number v-model="model.contractmoney" controls-position="right" :step="1" :min="0" style="width:100%" :disabled="true"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同金额(新)" prop="contractmoney2" :required="true" :rules="rules.validateRequire">
            <el-input-number v-model="model.contractmoney2" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="机会点编号" prop="chancebackbonecode" :rules="model.ishistorycontract == '1' ? { required: false } : rules.validateRequire" class="tl-ref-selector">
            <el-input v-model="model.chancebackbonecode" :disabled="true" >
              <el-button slot="append" icon="el-icon-search" @click="openRefChance"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机会点名称" prop="chancebackbonename" :rules="model.ishistorycontract == '1' ? { required: false } : rules.validateRequire" >
            <el-input v-model="model.chancebackbonename" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收件人(原)" prop="receiver">
            <el-input v-model="model.receiver" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收件人(新)" prop="receiver2" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.receiver2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收件人电话(原)" prop="phone">
            <el-input v-model="model.phone" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收件人电话(新)" prop="phone2" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.phone2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="回寄地址(原)" prop="postal">
            <el-input v-model="model.postal" type="textarea" :rows="2" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="回寄地址(新)" prop="postal2" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.postal2" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="快递方式(原)" prop="courier">
            <el-input v-model="model.courier" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="快递方式(新)" prop="courier2" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.courier2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="快递单号(原)" prop="couriernumber">
            <el-input v-model="model.couriernumber" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="快递单号(新)" prop="couriernumber2" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.couriernumber2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-collapse>
        <el-collapse-item title="【单据】相关信息">
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请部门" prop="deptname">
                <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请公司" prop="orgname">
                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请人" prop="username">
                <tl-ref-user v-model="model.username" :initDisplayData.sync="model.username" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请人工号" prop="usercode">
                <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单据日期" prop="docdate" :rules="rules.validateRequire">
                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交时间" prop="submitdate">
                <el-date-picker v-model="model.submitdate" type="datetime" placeholder="提交时间" disabled></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="审批流程" prop="flowid">
                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="state">
                <el-input v-model="model.dispstate" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <tl-approval-history ref="history" sourceType="PmContractChange" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave">
        <svg-icon icon-class="save" />保存
      </el-button>
      <tl-attachment ref="attachment" sourceType="PmContractChange" :sourceId="model.id" :disabled="disableAttachmentButton" btnTitle="合同文件"></tl-attachment>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
        <svg-icon icon-class="send" />提交审批
      </el-button>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableDelApproval" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button>
    </div>
    <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true" :externalQueryParam="{ qry_changeFlag: '1' }"></tlpm-project-ref>
    <tlpm-customer-ref ref="pmCustomerRef" @confirm="getpmCustomerRefData" :isSingleSelect="true"></tlpm-customer-ref>
    <tlpm-customer-ref ref="pmCustomerRef" @confirm="getpmCustomerRefDataNew" :isSingleSelect="true"></tlpm-customer-ref>
    <tl-chance-select :externalQueryParam="chanceParam" @confirm="afterSelectChance" ref="refChance"></tl-chance-select>
    <tlpm-contract-ref ref="pmContractRef" @confirm="getpmContractRefData" :isSingleSelect="true"></tlpm-contract-ref>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/refList.vue'
import tlpmCustomerRef from '@/views/ProjectManage/pmCustomer/refList.vue'
import tlpmContractRef from '@/views/ProjectManage/pmContract/refList.vue'
import tlChanceSelect from '../pmContract/components/chanceSelect'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
import { regionData } from '../../../utils/deal_area'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlpmProjectRef, tlpmCustomerRef, tlpmContractRef, tlChanceSelect },
  data() {
    return {
      moduleTitle: '合同信息变更申请',
      urlPrefix: '/pm/contractChange/',
      disableAttachmentButton: true,
      url: {
        // 额外的url
      },
      // 机会点参照选择 默认查询参数
      chanceParam: {}
    }
  },
  methods: {
    // 提交审批重写，先调用beforeApproval校验数据、附件等
    doSubmitApproval() {
      if (!this.url.submit) {
        this.$tap.alert('请设置审批地址!', 'warning')
        return
      }
      this.submitLoading = true
      if (!this.beforeApproval()) {
        return
      }
      this.$tap
        .confirm("确定提交审批吗？提交后，单据内容不可修改！")
        .then(_ => {
          putAction(this.url.submit, this.model).then(res => {
            if (res.success) {
              this.$tap.message(res.message, 'success')
              this.queryData(this.model.id)
              this.afterApproval()
            } else {
              this.$tap.alert(res.message, 'warning')
            }
            this.submitLoading = false
          })
        })
        .catch(_ => {
          this.submitLoading = false
        })
    },
    // 提交前执行
    beforeApproval() {
      let count = this.$refs.attachment.fileList.length > 0 ? this.$refs.attachment.fileList.length : this.$refs.attachment.fileCount
      if (this.model.contractmoney !== this.model.contractmoney2 && count <= 0) {
        this.$tap.alert('更改合同金额，请附件上传结算说明或其他证明文件', 'warning')
        this.submitLoading = false
        return false
      }
      return true
    },
    assignDefaultValue() {
      if (this.isAdd) {
        // if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
        //   this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        // }
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'docdate', new Date())
        this.$set(this.model, 'applytime', new Date())
      }
    },
    afterDelApproval() {
      this.disableAttachmentButton = false
    },
    loaded() {
      if (this.isAdd === false && this.disableEditSubmit === true) {
        var params = {
          sourceType: 'PmContractChange',
          sourceId: this.model.id
        }
        this.disableAttachmentButton = false
        getAction('/cbo/attachment/calculateCount', params).then((res) => {
          if (res.success) {
            if (res.result === 0) {
              this.disableAttachmentButton = true
              this.$emit('refresh')
            }
          }
        })
      }
      if (this.disableEditSubmit === false) {
        this.disableAttachmentButton = false
      }
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    /** 执行新增提交 */
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      this.$set(this.model, 'billuid', this.uid)
      postAction(this.url.add, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.queryData(res.result.id)
            this.disableAttachmentButton = false
            this.isAdd = false
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    },
    // /** 打开项目参照 */
    // openpmProjectRefDialog() {
    //   this.$refs.pmProjectRef.visible = true
    // },
    /** 打开合同名称参照 */
    openpmContractRefDialog() {
      this.$refs.pmContractRef.visible = true
    },
    getpmContractRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'projectid', selectionRow.projectid)
      // this.$set(this.model, 'dispprojectid', selectionRow.name)
      this.$set(this.model, 'contractid', selectionRow.id)
      this.$set(this.model, 'dispcontractid', selectionRow.contractname)
      // this.$set(this.model, 'contractname', selectionRow.id)
      this.$set(this.model, 'contractorgname', selectionRow.contractorgname)
      this.$set(this.model, 'contractdeptname', selectionRow.contractdeptname)
      this.$set(this.model, 'contractdate', selectionRow.contractdate)
      this.$set(this.model, 'contractmoney', selectionRow.contractmoney)
      this.$set(this.model, 'receiver', selectionRow.receiver)
      this.$set(this.model, 'phone', selectionRow.phone)
      this.$set(this.model, 'postal', selectionRow.postal)
      this.$set(this.model, 'courier', selectionRow.courier)
      this.$set(this.model, 'customername', selectionRow.customername)
      this.$set(this.model, 'couriernumber', selectionRow.couriernumber)

      this.$set(this.model, 'orgcode', selectionRow.contractorgcode)
      // this.$set(this.model, 'disporgcode', this.org.name)
      this.$set(this.model, 'orgname', selectionRow.contractorgname)
      this.$set(this.model, 'deptcode', selectionRow.contractdeptcode)
      this.$set(this.model, 'deptname', selectionRow.contractdeptname)

      // this.$set(this.model, 'deptcode', selectionRow.ownerdeptcode)
      // this.$set(this.model, 'deptname', selectionRow.ownerdeptname)
      // this.$set(this.model, 'orgcode', selectionRow.ownerorgcode)
      // this.$set(this.model, 'orgname', selectionRow.ownerorgname)
    },
    // 打开机会点选择界面
    openRefChance() {
      this.chanceParam = {}
      this.$refs['refChance'].visible = true
    },
    // 机会点参照赋值
    afterSelectChance(selectedRows, selectedKeys, selectedValues) {
      this.$set(this.model, 'chancebackbonecode', selectedRows.code)
      this.$set(this.model, 'chancebackbonename', selectedRows.projectname)
    }
    // /** 获取项目参数数据 */
    // getpmProjectRefData(selectionRow, selectKey, selectionValue) {
    //   this.$set(this.model, 'projectid', selectionRow.id)
    //   this.$set(this.model, 'dispprojectid', selectionRow.name)

    //   this.$set(this.model, 'contractorgname', selectionRow.contractorgname)
    //   this.$set(this.model, 'contractdeptname', selectionRow.contractdeptname)
    //   this.$set(this.model, 'contractname', selectionRow.contractname)

    //   this.$set(this.model, 'deptcode', selectionRow.ownerdeptcode)
    //   this.$set(this.model, 'deptname', selectionRow.ownerdeptname)
    //   this.$set(this.model, 'orgcode', selectionRow.ownerorgcode)
    //   this.$set(this.model, 'orgname', selectionRow.ownerorgname)
    // }
  }
}
</script>

